#ifndef _PACKETPARSERUTILS_H_
#define _PACKETPARSERUTILS_H_

#include <string>
#include <map>

class XmlPullParser;
class Packet;
class Presence;
class XMPPError;

class PacketParserUtils {
public:
	    
	/**
     * Parses a message packet.
     *
     * @param parser the XML parser, positioned at the start of a message packet.
     * @return a Message packet.
     * @throws Exception if an exception occurs while parsing the packet.
     */
	static Packet * parseMessage(XmlPullParser * parser) ;
	    
	/**
     * Parses a presence packet.
     *
     * @param parser the XML parser, positioned at the start of a presence packet.
     * @return a Presence packet.
     * @throws Exception if an exception occurs while parsing the packet.
     */
	static Presence * parsePresence(XmlPullParser * parser);
	
	/**
	 * Parse a properties sub-packet. If any errors occur while de-serializing Java object
	 * properties, an exception will be printed and not thrown since a thrown
	 * exception will shut down the entire connection. ClassCastExceptions will occur
	 * when both the sender and receiver of the packet don't have identical versions
	 * of the same class.
	 *
	 * @param parser the XML parser, positioned at the start of a properties sub-packet.
	 * @return a map of the properties.
	 * @throws Exception if an error occurs while parsing the properties.
	 */
    static std::map<std::string, std::string> parseProperties(XmlPullParser * parser);

	/**
     * Parses error sub-packets.
     *
     * @param parser the XML parser.
     * @return an error sub-packet.
     * @throws Exception if an exception occurs while parsing the packet.
     */
	static XMPPError * parseError(XmlPullParser * parser);

    /**
     * Parses a packet extension sub-packet.
     *
     * @param elementName the XML element name of the packet extension.
     * @param namespace the XML namespace of the packet extension.
     * @param parser the XML parser, positioned at the starting element of the extension.
     * @return a PacketExtension.
     * @throws Exception if a parsing error occurs.
     */
    static PacketExtension * parsePacketExtension(std::string elementName, std::string nameSpace, XmlPullParser * parser);

private:
	static std::string getLanguageAttribute(XmlPullParser * parser);

 	static std::string PROPERTIES_NAMESPACE;
};

//TODO move to utils
template <class T>
void convertFromString(T &value, const std::string &s) {
   std::stringstream ss(s);
   ss >> value;
};

std::string& trim(std::string &s)
{
    if (s.empty()) {
        return s;
    }
    s.erase(0,s.find_first_not_of(" "));
    s.erase(s.find_last_not_of(" ") + 1);
    return s;
} 

#endif // -- _PACKETPARSERUTILS_H_
